﻿Imports System.Text.RegularExpressions
Public Class HiddenForm3
    Dim countresults As Integer
    'Contains Yahoo Results

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim resultslist As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("div")
        Dim resultitem As HtmlElement
        Dim urla As String
        Dim name As String
        Dim temp As String
        Dim desc As String = MainSearchForm.txtSearchString.Text
        Dim desc1 As String = MainSearchForm.txtSearchString.Text
        countresults = 0

        For Each resultitem In resultslist
            If countresults < 10 Then
                If resultitem.GetAttribute("classname") = "res" Then
                    urla = resultitem.FirstChild.FirstChild.FirstChild.GetAttribute("href").ToString()
                    name = resultitem.FirstChild.FirstChild.FirstChild.InnerHtml
                    temp = Replace(name, "<B>", "")
                    name = Replace(temp, "</B>", "")

                    desc1 = resultitem.InnerHtml
                    Dim RegexObj As New Regex("<div .* class=abstr>(.*)</div>", RegexOptions.IgnoreCase)
                    Dim MatchResults As Match = RegexObj.Match(desc1)
                    While MatchResults.Success
                        desc = MatchResults.Groups(1).Value
                        temp = Replace(desc, "<b>", "")
                        desc = Replace(temp, "</b>", "")
                        temp = Replace(desc, "<B>", "")
                        desc = Replace(temp, "</B>", "")

                        MatchResults = MatchResults.NextMatch()
                    End While

                    MainSearchForm.apotelesmata.addResult(name, urla, "yahoo", desc)
                    countresults = countresults + 1
                    Try
                        MainSearchForm.ProgressBar1.Value = MainSearchForm.ProgressBar1.Value + 1
                    Catch
                    End Try
                End If
            Else
                Exit For
            End If
        Next

        resultslist = WebBrowser1.Document.GetElementsByTagName("a")
        For Each resultitem In resultslist
            If MainSearchForm.apotelesmata.yahoocount < 99 Then
                If resultitem.GetAttribute("id") = "pg-next" Then
                    urla = resultitem.GetAttribute("href").ToString()
                    WebBrowser1.Navigate(urla)
                    'Exit Sub
                End If
            Else
                MainSearchForm.flag3 = True
                MainSearchForm.ReadyToView()
                Exit For
            End If
        Next
    End Sub
End Class